<template>
	<view class="container">
		<view class="top-section">
			<view class="scan-section">
<!-- 				<view class="s-input-box">
					<input auto-focus="true" ref="locationInput" v-model="locationCode" class="scan-input"
						placeholder="请输入或扫描库位"/>
					<image v-if="locationCode" class="s-input-icon clear-icon" src="/static/images/clear.png" @click="clearLocationCode"></image>
					<image class="s-input-icon scan-icon" src="/static/images/scan.png"></image>
				</view> -->
				<view class="s-input-box">
					<input ref="shelvesInput" v-model="shelvesCode" class="scan-input"
						placeholder="请输入或扫描料架" @confirm="getShelves"/>
					<image v-if="shelvesCode" class="s-input-icon clear-icon" src="/static/images/clear.png" @click="clearShelvesCode"></image>
					<image class="s-input-icon scan-icon" src="/static/images/scan.png"></image>
				</view>
				<!-- <view class="s-input-box">
					<input ref="trayInput" v-model="trayCode" class="scan-input" @confirm="handleScan"
						placeholder="请输入或扫描托盘" />
					<image v-if="trayCode" class="s-input-icon clear-icon" src="/static/images/clear.png" @click="clearTrayCode"></image>
					<image class="s-input-icon scan-icon" src="/static/images/scan.png"></image>
				</view> -->
			</view>
		</view>
		<!-- Materials list -->
		<scroll-view scroll-y class="materials-list" v-if="materials.length">
			<view class="material-card" v-for="(item, index) in materials" :key="index">
				<!-- <view class="material-header">物料信息</view> -->
				<view class="material-content">
					<view class="material-row">
						<text class="label">托盘号</text>
						<text class="value">{{ item.trayNo }}</text>
					</view>
					<view class="material-row">
						<text class="label">物料名称</text>
						<text class="value">{{ item.productName }}</text>
					</view>
					<view class="material-row">
						<text class="label">物料数量</text>
						<text class="value">{{ item.qty }}</text>
					</view>
					<view class="material-row">
						<text class="label">批次</text>
						<text class="value">{{ item.batchNo }}</text>
					</view>
				</view>
			</view>
		</scroll-view>
		<view class="nodata-label" v-else>
			<text class="label">暂无数据</text>
		</view>

		<view class="fixed-bottom">
			<button class="submit-btn" @tap="handleSubmit">解 绑</button>
		</view>
	</view>
</template>

<script>
	import {
	  bindContainerNo,
	  unbindContainerNo,
	  getStockList
	} from "./api"
	export default {
		data() {
			return {
				locationCode: "",
				shelvesCode: "",
				trayCode: "",
				materials: []
			};
		},
		methods: {
			getShelves(){
				if(!this.shelvesCode){
					this.$modal.msg("请输入料架号")
					return
				}
				getStockList({
					containerNo: this.shelvesCode,
				}).then( res=>{
					// console.log(res)
					if(res.code == 200){
						this.materials = res.rows
					}else{
						res.msg && this.$modal.confirmClose(res.msg)
					}
				})
				
			},
			handleScan() {
				if (!this.locationCode || !this.shelvesCode) {
					this.$modal.msg("请扫描库位和货架信息")
					this.trayCode = ""
				}
			},
			clearLocationCode() {
				this.locationCode = '';
			},
			clearShelvesCode() {
				this.shelvesCode = '';
			},
			clearTrayCode() {
				this.trayCode = '';
			},
			clearInfo(){
				this.locationCode = ''
				this.shelvesCode = ''
				this.trayCode = ''
				this.materials = []
			},
			handleSubmit() {
				if (!this.shelvesCode) {
					this.$modal.msg("请扫描货架")
					return
				}
				this.$modal.confirm('确认解绑？').then(() => {
					unbindContainerNo({
						containerNo: this.shelvesCode,
					}).then(res => {
						if (res.code == 200) {
							this.$modal.confirmClose(res.msg)
						}else{
							res.msg && this.$modal.confirmClose(res.msg)
						}
						this.clearInfo()
					});
				})
			}
		}
	}
</script>

<style lang="scss">
	.container {
		min-height: 100vh;
		background-color: #F0F2F5;
		position: relative;
		padding-bottom: 240rpx; // 为固定底部区域留出空间

		.top-section {
			padding: 30rpx 20rpx;
			background: linear-gradient(180deg, #99cfd6 0%, #4599ad 100%) !important;
		}

		.scan-section {
			background-color: #FFFFFF;
			padding: 24rpx;
			border-radius: 16rpx;
			box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);
			margin-bottom: 20rpx;

			.s-input-box {
				position: relative;
				margin-bottom: 20rpx;

				&:last-child {
					margin-bottom: 0;
				}
				
				.scan-input {
					background-color: #F8F9FA;
					height: 80rpx;
					width: 100%;
					padding: 0 120rpx 0 24rpx;
					border-radius: 8rpx;
					font-size: 28rpx;
					border: 2rpx solid #E5E6EB;

					&::placeholder {
						color: #C9CDD4;
					}
				}

				.s-input-icon {
					position: absolute;
					width: 40rpx;
					height: 40rpx;
					top: 50%;
					transform: translateY(-50%);
					z-index: 1;
					
					&.clear-icon {
						right: 70rpx;
					}
					
					&.scan-icon {
						right: 20rpx;
					}
				}
			}
		}

		.info-section {
			background-color: #FFFFFF;
			padding: 24rpx;
			border-radius: 16rpx;
			box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);

			.info-item {
				display: flex;
				align-items: center;
				margin-bottom: 16rpx;

				&:last-child {
					margin-bottom: 0;
				}

				.info-label {
					font-size: 28rpx;
					color: #666;
					width: 120rpx;
				}

				.info-value {
					font-size: 28rpx;
					color: #333;
					font-weight: 500;
				}
			}
		}

		.materials-list {
			padding: 20rpx;
			max-height: calc(200vh - 800rpx);

			.material-card {
				background-color: #FFFFFF;
				border-radius: 16rpx;
				margin-bottom: 20rpx;
				box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);
				overflow: hidden;

				.material-header {
					padding: 20rpx 24rpx;
					background-color: #F8F9FA;
					font-size: 28rpx;
					color: #333;
					font-weight: 500;
					border-bottom: 2rpx solid #E5E6EB;
				}

				.material-content {
					padding: 24rpx;
				}

				.material-row {
					display: flex;
					justify-content: space-between;
					align-items: center;
					margin-bottom: 16rpx;
					font-size: 28rpx;

					&:last-child {
						margin-bottom: 0;
					}

					.label {
						color: #666;
					}

					.value {
						color: #333;
						font-weight: 500;
					}
				}
			}
		}

		.fixed-bottom {
			position: fixed;
			left: 0;
			right: 0;
			bottom: 0;
			background-color: #FFFFFF;
			padding: 24rpx 20rpx;
			box-shadow: 0 -4rpx 12rpx rgba(0, 0, 0, 0.05);

			.bottom-section {
				margin-bottom: 24rpx;

				.scan-title {
					font-size: 28rpx;
					color: #333;
					margin-bottom: 16rpx;
					font-weight: 500;
				}
			}
		}

		.submit-btn {
			height: 88rpx;
			line-height: 88rpx;
			background: #4599ad;
			color: #FFFFFF;
			border-radius: 10rpx;
			font-size: 32rpx;
			text-align: center;
			font-weight: 500;
			box-shadow: 0 4rpx 12rpx rgba(46, 92, 254, 0.3);

			&:active {
				transform: scale(0.98);
			}
		}

		.s-input-box {
			position: relative;
			display: flex;
			align-items: center;
			
			.s-input {
				flex: 1;
				padding-right: 100rpx;
			}
			
			.s-input-icon {
				width: 40rpx;
				height: 40rpx;
				
				&.clear-icon {
					margin: 0 10rpx;
				}
				
				&.s-i {
					margin-left: 10rpx;
				}
			}
		}
	}

	.nodata-label {
		text-align: center;
		margin-top: 40rpx;
		color: #999999;
	}
	
	.materials-list {
		padding: 20rpx;
		max-height: calc(200vh - 800rpx);
	
		.material-card {
			background-color: #FFFFFF;
			border-radius: 16rpx;
			margin-bottom: 20rpx;
			box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);
			overflow: hidden;
	
			.material-header {
				padding: 20rpx 24rpx;
				background-color: #F8F9FA;
				font-size: 28rpx;
				color: #333;
				font-weight: 500;
				border-bottom: 2rpx solid #E5E6EB;
			}
	
			.material-content {
				padding: 24rpx;
			}
	
			.material-row {
				display: flex;
				justify-content: space-between;
				align-items: center;
				margin-bottom: 16rpx;
				font-size: 28rpx;
	
				&:last-child {
					margin-bottom: 0;
				}
	
				.label {
					color: #666;
				}
	
				.value {
					color: #333;
					font-weight: 500;
				}
			}
		}
	}
</style>